package com.second.app.thread.waitold;

/**
 * @Author soul gao
 * @Date 2021/1/24 15:06
 * 努力学习 天天进步
 * 提前通知 并修改变量值 导致另外一个等待线程无法执行
 */
public class Main {


    public static void main(String[] args) {
        Object lock = new Object();

        final boolean[] flag = {true};

        new Thread(() -> {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + "开始通知：" + System.currentTimeMillis());
                lock.notify();
                flag[0] = false;
                System.out.println(Thread.currentThread().getName() + "结束通知：" + System.currentTimeMillis());
            }
        }, "线程BBB").start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            try {
                synchronized (lock) {
                    if (flag[0]) {
                        System.out.println(Thread.currentThread().getName() + "开始等待：" + System.currentTimeMillis());
                        lock.wait();
                        System.out.println(Thread.currentThread().getName() + "结束等待：" + System.currentTimeMillis());
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "线程AAA").start();


    }
}
